﻿namespace EyeOpen.Cart.Web
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using EyeOpen.Cart.Service;
    using EyeOpen.Cart.Web.ArticleLineServiceReference;

    public partial class _Default : System.Web.UI.Page
    {
        protected void OnArticleLineChanged(object sender, EventArgs e)
        {
            Control container = ((TextBox)sender).Parent;

            this.UpdateLineTotal(container);
            this.UpdateGlobalTotal();
        }

        private void UpdateGlobalTotal()
        {
            float total = 0;

            foreach (ListViewDataItem listViewDataItem in this.articleLineListListView.Items)
            {
                Label totalLabel = (Label)listViewDataItem.FindControl("totalLabel");
                total += float.Parse(totalLabel.Text);
            }

            this.articleTotal.Text = total.ToString();
        }

        private void UpdateLineTotal(Control container)
        {
            bool parsedDone = false;

            float price = this.ConvertToFloatOrShowError(container, "priceTextBox", out parsedDone);

            if (!parsedDone)
            {
                this.ShowError(container, "priceTextBox");
                return;
            }

            float quantity = this.ConvertToFloatOrShowError(container, "quantityTextBox", out parsedDone);

            if (!parsedDone)
            {
                this.ShowError(container, "quantityTextBox");
                return;
            }

            float iva = this.ConvertToFloatOrShowError(container, "ivaTextBox", out parsedDone);

            if (!parsedDone)
            {
                this.ShowError(container, "ivaTextBox");
                return;
            }

            if (parsedDone)
            {
                using (ArticleLineServiceClient articleLineServiceClient = new ArticleLineServiceClient())
                {
                    ArticleLineDto articleLineDto =
                        new ArticleLineDto
                        {
                            Price = price,
                            Quantity = quantity,
                            Iva = iva
                        };

                    Label totalLabel = (Label)container.FindControl("totalLabel");
                    totalLabel.Text = articleLineServiceClient.CalculateArticleLineTotal(articleLineDto).ToString();
                }
            }
        }

        protected void ArticleLineListListView_DataBound(object sender, EventArgs e)
        {
            this.UpdateGlobalTotal();
        }

        private float ConvertToFloatOrShowError(Control container, string textBoxId, out bool errorOccurred)
        {
            TextBox textBox = (TextBox)container.FindControl(textBoxId);

            float parsedValue;

            errorOccurred = float.TryParse(textBox.Text, out parsedValue);

            return parsedValue;
        }

        private void ShowError(Control container, string textBoxId)
        {
            this.articleTotal.Text = "Error";
        }
    }
}